home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 127_01.zip / FIO3.H < prev    next >
Text File  |  1993-06-17  |  8KB  |  234 lines

  1. /*********************************************************************\
  2. ** .---------------------------------------------------------------. **
  3. ** |                                                               | **
  4. ** |                                                               | **
  5. ** |         Copyright (c) 1981, 1982, 1983 by Eric Martz.         | **
  6. ** |                                                               | **
  7. ** |                                                               | **
  8. ** |       Permission is hereby granted to use this source         | **
  9. ** |       code only for non-profit purposes. Publication of       | **
  10. ** |       all or any part of this source code, as well as         | **
  11. ** |       use for business purposes is forbidden without          | **
  12. ** |       written permission of the author and copyright          | **
  13. ** |       holder:                                                 | **
  14. ** |                                                               | **
  15. ** |                          Eric Martz                           | **
  16. ** |                         POWER  TOOLS                          | **
  17. ** |                    48 Hunter's Hill Circle                    | **
  18. ** |                      Amherst MA 01002 USA                     | **
  19. ** |                                                               | **
  20. ** |                                                               | **
  21. ** `---------------------------------------------------------------' **
  22. \*********************************************************************/
  23.  
  24. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  25. /*
  26. "fio.3" INCLUDES fopen AND fclose, AND SHOULD BE INCLUDED BY THE LAST LINE
  27. IN THE MAIN SOURCE FILE. INCLUSION OF fio.3 IN MAIN IS NECESSARY TO GIVE THESE
  28. FUNCTIONS ACCESS TO NIOBUFS IN THE EXTERNAL VARIABLE bufuse[0], SINCE PASSING
  29. NIOBUFS AS AN ARGUMENT WOULD MAKE THE FUNCTIONS INCOMPATIBLE WITH STANDARD K&R
  30. C.
  31.  
  32. "bdscio.h" SHOULD BE MODIFIED TO INCLUDE THIS DEFINITION:
  33.  
  34. #define FILE struct _buf (SEE BDSCIO+.H)
  35.  
  36. There is one non-standard feature in the present fopen: if the function is
  37. called as 'fp = fopen(filename, "r");' and no such file exists, fopen will
  38. print an error message at the console before returning the error value of 0.
  39. If you wish to suppress this error message (e.g. your calling routine will
  40. detect the returned 0 and issue its own message) use a two character string as
  41. the mode, e. g. 'fp = fopen(filename, "r-");'.  The second character, which
  42. can be anything, causes fopen to suppress its internal error message.
  43.  
  44. N.B.! For the message suppression to work, putc.c in stdlib1.c must be
  45. modified to include the following line as the first executable statement:
  46.  
  47.     if (iobuf == 0) return(0); /* DUMP OUTPUT */
  48.  
  49. It is unfortunate that the BDSC functions supplied with the names "fopen" and
  50. "fclose" are not equivalent to the standard K&R functions. Therefore, in order
  51. to use the standard functions below, the BDSC-supplied functions must be
  52. renamed: fopen becomes r_open (open for read) and fclose becomes bf_close
  53. (close to free buffer). You must do this in your deff.crl using the rename
  54. command of clib. Unfortunately, this means that older programs which use the
  55. non-standard BDSC-supplied functions will have to be modified if they are to
  56. be relinked after these name changes.
  57.  
  58. If you wish to open for appending, you should use mode "a" in fopen, and
  59. compile and install the following function in your deff.crl:
  60.  
  61. #include "bdscio.h"
  62.  
  63. int a_open(filename,iobuf)
  64. struct _buf *iobuf;
  65. char *filename;
  66. {
  67.     if ((iobuf -> _fd = open(filename,2))<0) return ERROR;
  68.     iobuf -> _nleft = 0;
  69.     return iobuf -> _fd;
  70. }
  71.  
  72. Beware: CP/M does not like names like a_open.c; use aopen.c or a-open.c
  73. for the source file name, and then rename the crl module to a_open using
  74. clib.
  75. */
  76. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  77. /* Here is an example of how to include and use the FIO's in a source code:
  78.         
  79.         #include <bdscio.h>        
  80.  
  81.         #define NIOBUFS 1 /* or more, equal to the maximum number of
  82.                         files you will need to have open simultaneously;
  83.                         note that each takes about 1K of memory! */
  84.         #include <fio1.h>
  85.  
  86.         main(argc,argv)
  87.             int argc;
  88.             char **argv;
  89.             {
  90.             FILE *fp;
  91.             char filename[FILENAME];
  92.         #include <fio2.h>
  93.             /* code which gets filename goes here */
  94.             if (badname(filename)) exit(0); /* optional */
  95.             fp = fopen(filename, "w");
  96.             /* code which writes to file goes here */
  97.             fclose(fp);
  98.         }
  99.         #include <fio3.h>
  100.  
  101. */
  102. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  103.  
  104. FILE *fopen(filename,mode)
  105.     char *filename;
  106.     char *mode;
  107.     {
  108.     FILE *fp;
  109.     int i, bufat, Stderr;
  110.  
  111. #ifdef DEBUG
  112.     if (Debug) fprintf(STDERR, "-->Opening file \"%s\" (mode %s)",
  113.         filename, mode);
  114. #endif
  115.  
  116.     /* FIND UNUSED BUFFER */
  117.     for(bufat=1;bufuse[bufat] == 1;bufat++) {
  118.         if (bufat == bufuse[0]) {
  119.             printf(
  120.                 "\nFOPEN ERROR (%s): NO MORE BUFFERS (%d in use)\n",
  121.                 filename,bufuse[0]);
  122.             return(0);
  123.         }
  124.     }
  125.     /* ASSIGN FILE POINTER */
  126.     fp = _iob[bufat-1]; /* FOR EXPLANATION OF -1, SEE FILE FIO.1 */
  127.  
  128.     Stderr = stderr;
  129.     if (mode[1] > 0) Stderr = 0; /* DUMP ERROR MESSAGE */
  130.     switch (mode[0]) {
  131.         case 'r':
  132.             i = r_open(filename,fp);
  133.             if (i == ERROR) {
  134.                 fprintf(Stderr,
  135.                     "\nFOPEN ERROR: CAN'T R_OPEN \"%s\"\n",filename);
  136.                 return(0);
  137.             }
  138.             break;
  139.         case 'w':
  140.             i = fcreat(filename,fp);
  141.             if (i == ERROR) {
  142.                 fprintf(Stderr,
  143.                     "\nFOPEN ERROR: CAN'T FCREAT \"%s\"\n",filename);
  144.                 fprintf(Stderr,
  145. "This usually means there is no more room on the disk.\n");
  146.                 return(0);
  147.             }
  148.             break;
  149.         case 'a':
  150.             i = a_open(filename,fp);
  151.             if (i == ERROR) {
  152.                 fprintf(Stderr,
  153.                     "\nFOPEN ERROR: CAN'T A_OPEN \"%s\"\n",filename);
  154.                 return(0);
  155.             }
  156.             break;
  157.         default:
  158.             fprintf(Stderr,
  159.                 "\nFOPEN ERROR: BAD MODE '%s' FOR '%s'\n",
  160.                 mode,filename);
  161.             return(0);
  162.     }
  163.     bufuse[bufat] = 1; /* SET BUFFER USE FLAG */
  164.     
  165. #ifdef DEBUG
  166.     if (Debug) {
  167.         if (fp) fprintf(STDERR, " FD=%d\n", fp->_fd);
  168.         else fprintf(STDERR, " UNSUCCESSFUL\n");
  169.     }
  170. #endif
  171.     return(fp);
  172. }
  173. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  174. /* CLOSING STRATEGY:
  175.  
  176.     _fclose(fp) is the low level close which:
  177.         1. calls bf_close (which should be named bds_close)
  178.             which in turn flushes if(_WRITE) in _flags.
  179.         2. deallocates fio buffer.
  180.     
  181.     fpfree(fp) prevents a buffer flush in case file was opened
  182.         for append and only random writes were done.
  183.         
  184.     fclose(fp) appends ^Z and calls _fclose (assuming _flags
  185.         is as desired).
  186. */
  187. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  188. fpfree(fp)
  189.     FILE *fp;
  190.     {
  191.     /* TO AVOID FLUSHING BUFFER AFTER AN OPEN FOR APPEND
  192.     IN BDSC VERSION 1.5 */
  193.     fp->_flags = _READ;
  194.     
  195.     _fclose(fp);
  196. }
  197. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  198. _fclose(fp)
  199.     FILE *fp;
  200.     {
  201.     int bufat;
  202.  
  203. #ifdef DEBUG
  204.     if (Debug) fprintf(STDERR, "<--Closing FD=%d\n", fp->_fd);
  205. #endif
  206.  
  207.     bf_close(fp); /* BDS C */
  208.     
  209.     /* This next is required to fix a bug in CP/M. Use of putchar() can cause
  210.     problems when using DIO. */
  211.     bdos(2, 0); /* 2 = console out */
  212.  
  213.     for(bufat=1;fp != _iob[bufat-1];bufat++); /* FIND bufuse INDEX */
  214.     bufuse[bufat] = 0; /* SHOW BUFFER NO LONGER IN USE */
  215.     return(0);
  216. }
  217. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  218. fclose(fp)
  219.     FILE *fp;
  220.     {
  221.     putc('\032',fp); /* CTRL Z */
  222.  
  223. /*    fflush(fp);    NOT NECC FOR BDSC 1.5 WHICH CALLS IT IN BF_CLOSE
  224.  
  225.     THE FOLLOWING MOVED TO FPFREE FOR BDSC 1.5
  226.     putchar('\0'); /* This is required to fix a bug in CP/M */ 
  227. */
  228.     _fclose(fp);
  229. }
  230. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  231. /*    END OF FIO3.H    */
  232. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  233.  
  234. - - - - - - - - - - - - - - - - - -